package com.x.xiaoshuo.ui.read;

import com.x.mvp.base.recycler.PullToRefreshRecyclerActivityPresenter;
import com.x.service.api.DataRepository;
import com.x.service.entity.BookListsBean;
import com.x.service.entity.ChapterList;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


class ChaptersPresenter extends PullToRefreshRecyclerActivityPresenter<ChaptersActivity> {
    DataRepository dataRepository;
    ChapterList chapterList;
    BookListsBean bookListsBean;
    public static final String INTENT_BOOKID = "INTENT_BOOKID";
    public static final String INTENT_SCREEN_OR = "INTENT_SCREEN_OR";

    @Inject
    ChaptersPresenter(DataRepository dataRepository) {
        this.dataRepository = dataRepository;
    }

    @Override
    public void loadData() {
        bookListsBean = (BookListsBean) view.getIntent().getSerializableExtra(INTENT_BOOKID);
        if (bookListsBean != null) {
            getChapterLst(bookListsBean._id);
            view.bookId = bookListsBean._id;
        }
    }

    public void getChapterLst(String bookId) {
        view.showAppProgress();
        addSubscription(dataRepository.getChapters(bookId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(chapterList -> {
                    chapterList = chapterList;
                    view.showBookToc(chapterList.chapterInfo.chapters);
                    view.hideAppProgress();
                }, throwable -> {
                    view.hideAppProgress();
                }, () -> {
                    view.hideAppProgress();
                }));
    }

    @Override
    public void loadMore(int totalItemCount) {

    }

    void gotoRead(int chapter) {
        ReadActivity.startActivity(view, bookListsBean, chapter + 1);
    }
}
